package com.ksd.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.course.entity.AdminCourseLesson;
import com.ksd.modules.course.mapper.AdminCourseLessonMapper;
import com.ksd.modules.course.po.AdminCourseLessonSavePO;
import com.ksd.modules.course.service.IAdminCourseLessonService;
import com.ksd.modules.course.vo.AdminCourseLessonVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class AdminCourseLessonServiceImpl extends BaseServiceImpl<AdminCourseLessonMapper, AdminCourseLesson> implements IAdminCourseLessonService {

    /**
     * 查询列表
     *
     * @param courseId
     * @param chapterId
     * @return
     */
    public List<AdminCourseLessonVo> findAdminCourseLessons(Long courseId, Long chapterId) {
        LambdaQueryWrapper<AdminCourseLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseLesson::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminCourseLesson::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminCourseLesson::getCourseId, courseId);
        lambdaQueryWrapper.eq(AdminCourseLesson::getChapterId, chapterId);
        lambdaQueryWrapper.orderByAsc(AdminCourseLesson::getSorted);
        return tranferListBo(this.list(lambdaQueryWrapper), AdminCourseLessonVo.class);
    }


    /**
     * 保存和更新
     *
     * @param adminCourseLessonSavePO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUpdateAdminCourseLesson(AdminCourseLessonSavePO adminCourseLessonSavePO) {
        AdminCourseLesson adminCourseLesson = tranferBo(adminCourseLessonSavePO, AdminCourseLesson.class);
        adminCourseLesson.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminCourseLesson.setSystemName(SystemIdThrealLocal.get().getSystemName());
        return this.saveOrUpdate(adminCourseLesson);
    }

    /**
     * 根据ID获取明细
     *
     * @param id
     * @return
     */
    public AdminCourseLessonVo getAdminCourseLessonById(Long id) {
        AdminCourseLesson adminCourse = this.getById(id);
        AdminCourseLessonVo adminCourseVo = tranferBo(adminCourse, AdminCourseLessonVo.class);
        return adminCourseVo;
    }


    /**
     * 单个物理删除
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeAdminCourseLessonById(Long id) {
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, String column, Integer value) {
        boolean b = super.updateStatus(id, column, value);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id, String column, String value) {
        boolean b = super.updateStatusStr(id, column, value);
        return b;
    }

    /**
     * 根据章删除节
     * @param chapterId
     * @return
     */
    @Override
    public boolean removeByChapterId(Long chapterId){
        LambdaQueryWrapper<AdminCourseLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseLesson::getChapterId,chapterId);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 根据课程删除节
     * @param courseId
     * @return
     */
    @Override
    public boolean removeByCourseId(Long courseId){
        LambdaQueryWrapper<AdminCourseLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseLesson::getCourseId,courseId);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 根据课程删除节
     * @param courseIds
     * @return
     */
    @Override
    public boolean removeByCourseIds(List<Long> courseIds){
        LambdaQueryWrapper<AdminCourseLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AdminCourseLesson::getCourseId,courseIds);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminCourseLesson data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
